<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Cookie;
use think\Request;
use app\index\model\Nav;
use app\index\model\Type;
use app\index\model\Course;
use app\index\model\Specific;
use app\index\model\KnowUserLog;
use app\index\service\CourseService;


class Index  extends Common
{
	public function index(){
		$typeData = Type::getNavData(2);
		$sData = Specific::getWhereData(['nav_id'=>2]);
		$sData = CourseService::disposeCourseType($sData, 'type_id');
		return $this->fetch('',[
			'fetch' => 'index',
			'sData' => $sData,
			'typeData' => $typeData,
		]);
	}


	public function course(Request $request){
		$param = $request->param();
		$navData = Nav::getOneData($param['nav_id']);
		if(!$param['nav_id'] || !$navData) die($this->checkParamId());
		$typeData = Type::getNavData($param['nav_id']);
		$courseData = Course::getWhereData(['nav_id' => $param['nav_id']]);
		$courseData = json_decode( json_encode($courseData), true);
		if (!$courseData) die($this->checkParamId());
		$pointSum = returnCourseSumField($courseData);
		if($param['type_id']){
			$fetch = 'type';
			$typeOne = Type::getOneData($param['type_id']);
			$typeOne['master'] = CourseService::getTypeMaster($param, $courseData);
			$sData = CourseService::disposeCourseSpec($param, $courseData);
		}else{
			$fetch = 'course';
			$sData = Specific::getWhereData(['nav_id' => $param['nav_id']]);
			$lastLog = KnowUserLog::getUserCount(['nav_id'=>$param['nav_id']], 'find', '', 'know_user_log.updated_time desc');
			$typeData = CourseService::disposeCourse($typeData, $sData, $courseData, 'type_id');
		}
		return $this->fetch($fetch,[
			'param' => $param,
			'sData' => $sData,
			'lastLog' => $lastLog,
			'navData' => $navData,
			'typeOne' => $typeOne,
			'typeData' => $typeData,
			'pointSum' => $pointSum,
			'courseData' => $courseData,
			'title' => ($param['type_id'] ? $typeOne['type_name'].'-' : ''). $navData['nav_name'].'-学习中心',
		]);
	}
}